var pageNum = null;
var pageSize = 5;
let vm = new Vue({
        el: '.main-content',
        data: function () {
            return {
                pageInfo: {},
                name: "全部",
                condition: {
                    name: '',
                    id: '',
                },
                setting: {
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'parentId',
                        },
                    },
                    callback: {
                        onClick: this.onClick
                    },
                    view: {
                        fontCss: this.fontCss
                    }
                },

                nodes: []
            }
        },
        methods: {

            getPage: function (pageNum, pageSize) {
                selectPage(pageNum, pageSize)
            },

            selectPage2: function (pageNum = 1, pageSize = 5) {
                axios({
                    url: `/guguanjia/role/selectPage/${pageNum}/${pageSize}`,
                    params: this.condition
                }).then(response => {
                    vm.$data.pageInfo = response.data;
                }).catch(error => {
                    console.log(error.message)
                })
            },
            selectAll: function () {
                this.condition.id = '';
                this.condition.name = '';
                this.name = "全部";
                this.selectPage2()
            },

            toUsers: function (id, name) {
                layer.obj = id;
                layer.obj = name;
                layer.open({
                    type: 2,      //表示弹出一个iframe
                    title: false,  //是否设置标题
                    area: ['90%', '90%'],  //设置子窗口的宽高
                    content: '/guguanjia/role/toUser', //iframe的url
                })
            },

            toUpdate: function (id) {
                layer.obj = id;
                console.log("id:" + id);
                layer.open({
                    type: 2,
                    title: '编辑',  //是否设置标题
                    closeBtn: 1, //不显示关闭按钮
                    area: ['70%', '70%'],
                    content: '/guguanjia/role/toUpdate',
                    success: function (aa, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        iframe.abcdefgh(id)
                    },
                    end: function () {

                    }
                })
            },

            //删除
            doDelete: function (role) {
                layer.msg('是否删除', {
                    time: 20000, //20s后自动关闭
                    btn: ['是', '否'],
                    yes: () => {
                        //删除标记（0：正常；1：删除；2：审核；）
                        role.delFlag = "1";
                        axios({
                            url: "/guguanjia/role/updateById",
                            method: "POST",
                            data: role,
                        }).then(response => {
                            layer.msg("删除成功")
                            this.getPage(pageNum, pageSize)
                            // this.selectPage();//刷新页面
                        }).catch(error => {
                            layer.msg(error.message)
                        });
                    }
                });
            },

            initTree: function () {
                //初始化nodes数据
                axios({
                    url: "/guguanjia/office/findAll"
                }).then(response => {
                    this.nodes = response.data;
                    this.nodes[this.nodes.length] = {"id": 0, "name": "全部"};
                    $.fn.zTree.init($("#pullDownTreeone"), this.setting, this.nodes)
                }).catch(error => {
                    layer.msg(error.message)
                })

            },
            onClick: function (event, treeld, treeNode) {
                this.name = treeNode.name;
                if (treeNode.id === 0) {
                    //如果选择的是全部
                    this.condition.id = null;
                } else {
                    //绑定查询条件
                    this.condition.id = treeNode.id;
                }
                //删除open
                $(".btn-group").removeClass("open");

            },
            search: function () {
                //根据树id获取树对象
                let treeObj = $.fn.zTree.getZTreeObj("pullDownTreeone");

                //初始化树节点，防止之前设置的high届性影响
                //transformToArray :将多维结构节点数组，转换成简单数纽―维结构
                let arrayNodes = treeObj.transformToArray(treeObj.getNodes());
                for (let i = 0; i < arrayNodes.length; i++) {
                    arrayNodes[i].hightLight = false;
                    //更新节点，触发调用 fontCss 方法
                    treeObj.updateNode(arrayNodes[i])
                }


                //getNodesByParamFuzzy ( key , value, parentNode ) :根据key指定的届性，value指定的值
                // 从指定的parentNode节点中模糊查找parentNode为null则全部查找
                let nodesFuzzy = treeObj.getNodesByParamFuzzy("name", this.name, null);

                //将所有查找到的元素设置为高亮
                for (let i in nodesFuzzy) {
                    nodesFuzzy[i].hightLight = true;

                    //更新节点，触发调用 fontCss 方法
                    treeObj.updateNode(nodesFuzzy[i])
                }


            },
            fontCss: function (treeId, treeNode) {
                // 根据高亮标记设置高亮红色
                return treeNode.hightLight ? {"color": "red"} : {"color": "black"}

            },

        },
        created: function () {
            selectPage(1, 5);
            this.initTree()
        }

    })
;

function selectPage(pageNum, pageSize) {
    this.pageNum = pageNum;
    this.pageSize = pageSize;
    axios({
        url: `/guguanjia/role/selectPage/${pageNum}/${pageSize}`,
    }).then(response => {
        vm.$data.pageInfo = response.data;
    }).catch(error => {
        console.log(error.message)
    })
}